from jili.core.ftp import sftp,ftp_size
from jili.core.printlog import print
import os
import datetime
import zipfile
from jili.tool.convert import str2datetime
from jili.data.db import getdb_client

def unzip(zip_url,out_url=None,compresslevel=5,objs=[]):
    if out_url is None:
        out_url=zip_url.replace(".zip","")
    try:
        with zipfile.ZipFile(zip_url, "r", zipfile.ZIP_DEFLATED,compresslevel=compresslevel) as f:
            names=f.namelist()
            for i in names:
                if objs:
                    obj=i.split("_")[0]
                    if obj in objs:
                        out_url0=os.path.join(out_url,obj)
                        f.extract(i,out_url0)
                else:
                    obj = i.split("_")[0]
                    out_url0 = os.path.join(out_url, obj)
                    f.extract(i, out_url0)
            #f.extractall(out_url)  ##将文件解压到指定目录，解压密码为root
    except Exception as e:
        print("异常对象的内容是:%s" % e)
    finally:
        f.close()
def deal_unzip(local_url,url,filter_pcode):
    db = getdb_client()
    url=os.path.abspath(url)
    d = url.split(os.sep)[-1].split(".")[0]
    d = str2datetime(d)
    objs = []
    for i in db["future_continuous_k1d"].find({"timekey": d, "pcode": {"$in": filter_pcode}}, {"trade_obj": 1}):
        objs.append(i["trade_obj"])
    for i in db["future_master_k1d"].find({"timekey": d, "pcode": {"$in": filter_pcode}}, {"trade_obj": 1}):
        obj=i["trade_obj"]
        if obj not in objs:
            objs.append(obj)
    out_url = os.path.join(local_url, "future")
    print(url)
    if objs:
        unzip(zip_url=url, out_url=out_url,objs=objs)
    print("unzip",url, out_url)
    db.client.close()
    # for pkl in os.listdir(out_url):
    #     url0 = os.path.join(out_url, pkl)
    #     obj = pkl.split("_")[0]
    #     url2=os.path.join(local_url, "future", obj)
    #     if not os.path.exists(url2):
    #         os.mkdir(url2)
    #     url1 = os.path.join(local_url, "future", obj, pkl)
    #     shutil.move(url0, url1)
    #     # print("move",url0, url1)
    # shutil.rmtree(out_url)
    # print("rmtree", out_url)
def download(ip,password,port = 22,username = "root",local_url=r"D:\data",remote_url=r"/mnt/data/pkl",isunzip=True,isremove_remote=False,filter_pcode=[]):
    sftp0 = sftp(ip, username, password,port)

    sftp0.chdir(remote_url)
    zips=sftp0.listdir()
    if not os.path.exists(local_url):
        os.mkdir(local_url)
    zips0=os.listdir(local_url)

    for i in zips:
        if ".zip" not in i:
            continue
        if i in zips0:
            z0 = ftp_size(sftp0,remote_url=remote_url + "/" + i)
            z1 = os.path.getsize(os.path.join(local_url, i))
            print("0.check size,remote", z0, "local", z1)
            if z0 is not None:
                if z0 != z1:
                    print("0.文件不一致,删除本地", i)
                    os.remove(os.path.join(local_url, i))
                else:
                    print("0.文件一致,删除云端", i)
                    sftp0.remove(i)
                    continue
        print(i)
        url=os.path.join(local_url,i)
        sftp0.get(i, url)
        print("download",os.path.join(local_url,i))
        z0=ftp_size(sftp0,remote_url=remote_url+"/"+i)
        z1=os.path.getsize(os.path.join(local_url,i))
        print("check size,remote",z0,"local",z1)
        if z0 is not None:
            if z0!=z1:
                print("文件不一致,删除本地",i)
                os.remove(os.path.join(local_url,i))
            else:
                if isremove_remote:
                    sftp0.remove(i)
                    print("remove",remote_url+"/"+i)
                if isunzip:
                    if filter_pcode:
                        deal_unzip(local_url,url,filter_pcode=filter_pcode)
    return datetime.datetime.now
